<?php include_once 'include/header.php'; ?>
<?php include_once 'postcontroller.php'; ?>
<?php include_once 'commentcontroller.php'; ?>
<?php include_once 'albumcontroller.php'; ?>
<style>
    /* This rule is read by Galleria to define the gallery height: */
            #galleria{height:320px;}
</style>
<div id="content">
    <?php include_once 'include/leftsidebar.php' ?>
    <div id="cmiddle">        
        <div id="content-middle" style="position: relative">                                      
<!--            <img src="Images/icon/ribbon.png" style="position: absolute;top:0px;left:0px;z-index: 999999"/>-->
            <div id="galleria">
                <?php foreach($list_random_img as $img){ ?>
                <a href="data/album/<?php echo $img["image_name"] ?>">
                    <img title="Locomotives Roundhouse"
                         alt="Steam locomotives of the Chicago &amp; North Western Railway." 
                         src="data/album/<?php echo $img["image_name"] ?>">
                </a>                
                <?php } ?>
            </div>            
        </div>
        <div class="clear"></div>
        <div  style="line-height: 36px;height:36px;border-bottom:1px solid #ccc;margin-top:30px;background: url('Images/icon/news_logo.png') no-repeat 5px center">
            <b style="font-size: 1.5em;padding-left:40px">Bảng tin 52PM1</b>

            <div style="float:right">
            <g:plusone  href="http://52pm1.nuce.edu.vn"></g:plusone>
            </div>
        </div> 

        <div style="margin:5px;">                
            <b style="color:#c53f24">Hãy chia sẻ gì đó với các thành viên 52PM1:</b> <img style="position: relative;top:5px;left:15px" src="Images/icon/comment_ico.png"/>
            <span class="ajax-load" style="margin-left:20px"></span>
            <br/><br/>
            <form action="" id="form-post" method="POST">
                <input type="hidden" name="newpost" value="newpost"/>
			<?php if(isset($_COOKIE["username"])){ ?>
                <textarea class="borderinput" id="newpost" placeholder="Thêm chia sẻ mới..." style="width:500px;height:25px" name="title"></textarea>
			<?php }else{ ?>
				<textarea class="borderinput" placeholder="Đăng nhập hoặc đăng ký để bình luận..." style="width:500px;height:20px" name="title"></textarea>
			<?php } ?>
            </form>
        </div>
        <?php
        foreach ($list_post as $row) {            
            //lay so nguoi Comment
            $dem_comment = get_count_comment($row[0]);
            ?>
            <div class="content-post-comment" style="border-bottom: 1px solid #ccc;padding-bottom:8px;">
                <div class="post-item">
                    <div style="float:left;margin:10px;">
                        <img width="48px" height="48px" class="img-border" src="data/avatar/<?php echo $row["avatar"] ?>"/><br/>
                        <?php if($row["user_id"] == $_COOKIE["id"]){ ?>
                            <a class="delete_post link" pid="<?php echo $row[0] ?>" href="#">Xóa bài này</a>
                            <?php } ?>
                    </div>
                    <div style="margin:10px;float:left;width:400px;word-wrap:break-word;">
                        <b class="tipsy" title="Chi tiết thành viên: <?php echo $row["fullname"] ?>" style="color:#3d4fbb;cursor: pointer"><?php echo $row["fullname"] ?></b>
                        <br/>
                        <br/>
                        <span>
                            <?php echo Util::substr($row["title"],200) ?>
                        </span>
                        <br/><br/>
                        <span style="margin-top:5px;color:#a11818">
                            <?php $date = date("F j, Y, g:i a", strtotime("{$row["date_post"]}")); ?>
                            <?php echo $date ?> . <img src="Images/icon/comment.gif"/> 
                            <span class="comment-load" style="color:#3d4fbb"><?php echo $dem_comment ?></span>     
                            .<a class="link" href="comment.php?pid=<?php echo $row[0] ?>">Chi tiết </a>                            
                        </span>
                    </div>
                    <div style="clear: both"></div>
                </div>
                <div class="comment-content" style="width:522px;position: relative">
                    <img src="Images/icon/arrow.png" style="position: absolute;left:97px;top:-4px;"/>
                    <ul class="comment-item cm_<?php echo $row[0] ?>" style="margin-left:84px;">
                        <?php if ($dem_comment > 3) { ?>
                            <li>                                
                                <a class="link showcomment" pid="<?php echo $row[0] ?>" href="#">Xem tất cả <?php echo $dem_comment; ?> Bình luận</a>                                
                            </li>
                        <?php }else{ ?>
                            <li>
                                <a class="link" href="comment.php?pid=<?php echo $row[0] ?>">Xem chi tiết bài viết </a>                                
                            </li>
                        <?php } ?>
                        <?php
                        $list_comment = get_comment_by_posts($row[0]);
                        $showall = get_comment_show_all($row[0]);
                        if (!empty($list_comment)) {          
                            $dem = 0;
                            foreach ($list_comment as $item) {       
                                $dem ++;                                
                                ?>       
                            
                                <li class="cm<?php echo $item[18] ?> socm<?php echo $dem ?>_<?php echo $row[0] ?> cm_by_post<?php echo $row[0] ?>">
                                    <?php if($item[0] == $_COOKIE["id"]){ ?>
                                    <span class="tipsy delete_comment" id="<?php echo $item[18] ?>" title="Xóa comment này" style="padding:1px;background: #fff;border:1px solid #ccc;cursor:pointer;">x</span>
                                    <?php } ?>
                                    <div style="float:left">
                                        <img width="28px" height="28px" class="img-border" src="data/avatar/<?php echo $item["avatar"] ?>"/>
                                    </div>
                                    <div style="float: left;padding-left:5px;width:376px;word-wrap:break-word">
                                        <b class="tipsy" title="Chi tiết thành viên: <?php echo $item["fullname"] ?>" style="color:#286fc7;cursor: pointer" onclick="window.location = 'member.php?uid=<?php echo $item[0] ?>'"><?php echo $item["fullname"] ?></b>
                                        <?php echo $item["content"] ?>
                                        <br/><br/>
                                        <?php $date = date("F j, Y, g:i a", strtotime("{$item["date_comment"]}")); ?>
                                        <span style="color:#c76728">Đăng lúc <?php echo $date ?></span>
                                    </div>
                                    <div style="clear: both"></div>
                                </li>                                       
                        <?php } ?>
                                <script type="text/javascript">
                                    var socm = $(".cm_by_post<?php echo $row[0] ?>").length;                                                       
                                     if(parseInt(socm) > 3){                                     
                                      for(i=0;i<=socm-3;i++){                                         
                                            $(".socm"+i+"_"+<?php echo $row[0] ?>).hide();
                                        }  
                                    }else{
                                        $(".cm_by_post<?php echo $row[0] ?>").show();
                                    } 
                                </script>   
                                <?php } ?>
                    </ul>
                    <ul class="comment-item" style="margin-left:84px;">
                        <li class="comment-input_<?php echo $row[0] ?>">
                        <?php if(isset($_COOKIE["username"])){ ?>
    <!--                                <form action="" method="POST">-->
                            <input type="text" class="borderinput newcomment comment_<?php echo $row[0] ?>" postid="<?php echo $row[0] ?>" placeholder="Viết bình luận của bạn..." style="width:422px;height:20px"/> 
    <!--                                </form>-->
                                <?php }else{ ?>
                                <input type="text" class="borderinput"  placeholder="Đăng nhập hoặc đăng ký để bình luận..." style="width:422px;height:20px"/> 
                                <?php } ?>
                        </li>
                    </ul>
                </div>
            </div>
        <?php } ?>
        <div class="paging" style="text-align: center;margin-top:10px;height:25px;"><?php echo $page->firs_page ?><?php echo $page->previous_page ?><?php echo "$page->page_links"; ?><?php echo $page->next_page ?><?php echo $page->last_page ?></div>
    </div>
    <?php include_once 'include/rightsidebar.php'; ?>
</div>
<script type="text/javascript">
    Galleria.loadTheme('js/galleria.classic.min.js');
        
    $('#galleria').galleria({
        autoplay: 5000
    });
    
    if (document.getElementById("newpost")) {
        document.getElementById("newpost").onkeydown = function(e) {
            e = e || window.event;
            if (e.keyCode == 13) {
//                if($("#newpost").val()!= ""){
//                    $.ajax({
//                        url:"postcontroller.php?action=addnewpost",
//                        type:"POST",
//                        data:$("#form-post").serialize(),
//                        beforeSend:function(){
//                          $(".ajax-load").html("<img src='Images/icon/ajax-loader.gif'/>");  
//                        },
//                        success:function(data){
//                            if(data == true){
//                                window.location = window.location;
//                            }
//                        }                           
//                    });
                    $("#form-post").submit();
               // }
                return false;
            }
        };
    }
    $(function(){
        
        $(".showcomment").click(function(){
            var pid = $(this).attr("pid");            
            $(".cm_by_post"+pid).show();
            return false;
        })
        $(".delete_post").live('click',function(){
                var id = $(this).attr("pid");
                if(confirm("Bạn có chắc chắn muốn xóa chia sẻ này?")){
                $.ajax({
                    url:"postcontroller.php?action=delete_post",
                    type:"GET",
                    data:{id:id},
                    success:function(data){
                        if(data == true){
                            window.location = 'index.php';
                        }
                    }
                });
                }
            });        
    $(".delete_comment").live('click',function(){
            var id = $(this).attr("id");
            if(confirm("Bạn có chắc chắn muốn xóa comment này?")){
            $.ajax({
                url:"commentcontroller.php?action=delete_comment",
                type:"GET",
                data:{id:id},
                success:function(data){
                    if(data == true){
                        $(".cm"+id).fadeTo("slow", 0);
                        $(".cm"+id).slideUp("slow");
                    }
                }
            });
            }
        });
        $(".newcomment").keydown(function(e){               
            e = e || window.event;            
            if (e.keyCode == 13) {
                var postid = $(this).attr("postid");
                if($(".comment_"+postid).val()!= ""){
                    $.ajax({
                        url:"commentcontroller.php?action=addnewcomment&postid="+postid,
                        type:"POST",
                        data:{content:$(".comment_"+postid).val()},
                        success:function(data){

                            $(".cm_"+postid).append(data);
                            $(".cm_"+postid+" li:last").hide().slideDown("slow");
                            $(".comment-input_"+postid+" input").val("");
                        }                           
                    });
                }
            }
        })
    })
   
    /*if (document.getElementById("newpost")) {
            document.getElementById("newpost").onkeydown = function(e) {
                e = e || window.event;
                if (e.keyCode == 13) {
                    if($("#newpost").val()!= ""){
                        $.ajax({
                           url:"controllers/postcontroller.php?action=addnewpost",
                           type:"POST",
                           data:$("#form-post").serialize(),
                           success:function(data){
                             if(data == true){
                                 window.location = window.location;
                             }
                           }                           
                        });
                    }
                    return false;
                }
            };
        }*/
</script>

<?php include_once 'include/footer.php' ?>